﻿using System;
using System.IO;
using System.Web;
using System.Web.Hosting;
using System.Web.UI;

namespace MPBlog.Core
{
    /// <summary>
    /// 所有页面的基类
    /// 1.根据当前设置动态指定其母版页
    /// </summary>
    public class PageBase : Page
    {

        #region - field -

        /// <summary>
        /// 主题文件中要显示的用户控件的名字
        /// </summary>
        protected string PageName { get; set; }

        #endregion

        #region - event -

        //动态加载主题
        protected override void OnPreInit(EventArgs e)
        {
            string theme = Request.QueryString["theme"];
            if (IsAdminRequest)
            {

                if (string.IsNullOrEmpty(theme))
                {
                    MasterPageFile = string.Format("{0}admin/themes/{1}/admin.master", Utils.RelativeWebRoot, Blog.AdminTheme);
                }
                else
                {
                    //url中指定了要显示的主题
                    string file = string.Format("~/Admin/Themes/{0}/admin.master", theme);
                    bool fileExits = File.Exists(HostingEnvironment.MapPath(file));
                    MasterPageFile = string.Format("{0}/admin/themes/{1}/admin.master", Utils.RelativeWebRoot, fileExits ? theme : Blog.AdminTheme);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(theme))
                {
                    MasterPageFile = string.Format("{0}themes/{1}/main.master", Utils.RelativeWebRoot, Blog.BlogTheme);
                }
                else
                {
                    string file = string.Format("~/Themes/{0}/main.master", theme);
                    bool fileExits = File.Exists(HostingEnvironment.MapPath(file));
                    MasterPageFile = string.Format("{0}themes/{1}/main.master", Utils.RelativeWebRoot, fileExits ? theme : Blog.BlogTheme);
                }
            }

            base.OnPreInit(e);
        }

        //将指定的PageName的用户控件加入到母版页中
        protected override void OnLoad(EventArgs e)
        {
            //动态将需要显示的用户控件添加到本页面容器中
            string query = Request.QueryString["theme"];
            string theme = !string.IsNullOrEmpty(query) ? query : IsAdminRequest ? Blog.AdminTheme : Blog.BlogTheme;
            string path = string.Format(IsAdminRequest ? "{0}Admin/themes/{1}/{2}.ascx" : "{0}themes/{1}/{2}.ascx", Utils.RelativeWebRoot, theme, PageName);
            if (File.Exists(HostingEnvironment.MapPath(path)))
            {
                if (Page.Master != null)
                {
                    Control contentControl = LoadControl(path);
                    if (contentControl != null)
                    {
                        contentControl.ID = PageName;
                        Control bodyHolder = Page.Master.FindControl("bodyHolder");
                        if (bodyHolder != null)
                        {
                            bodyHolder.Controls.Add(contentControl);
                        }
                    }
                }
            }
            base.OnLoad(e);
        }

        #endregion


        #region - method -

        /// <summary>
        ///  判断当前是否请求Admin下的页面
        /// </summary>
        private bool IsAdminRequest
        {
            get
            {
                return Request.Url.AbsolutePath.ToLower().Contains("/admin/");
            }
        }

        /// <summary>
        /// 返回当前主题文件夹根目录的绝对路径，以/结尾
        /// </summary>
        public string CurrentThemeAbsoluteRoot(string themeName)
        {
            return Utils.AbsoluteWebRoot + "Admin/Themes/" + themeName + "/";
        }

        /// <summary>
        /// 返回admin文件夹的绝对地址
        /// </summary>
        /// <returns></returns>
        public string AdminAbsoluteRoot()
        {
            return Utils.AbsoluteWebRoot + "Admin/";
        }

        #endregion
    }
}